'''
Created on 19/03/2009

@author: eh01
'''
from yarra.event.event import Event
from yarra.event.eventhandler import BaseEventHandler
from yarra.event.eventmanager import EventManager
import unittest



class Test(unittest.TestCase):


    def setUp(self):
        pass


    def tearDown(self):
        pass


    def testEventManagerCase1(self):
        """ 
        tests that a handler can add events to the queue and that they
        are handled successfully
        """
        # override the default get_eventHandlers method at a class level for testing purposes
        instancemethodEventHandlers = type (EventManager.get_eventHandlers)
        EventManager.get_eventHandlers = instancemethodEventHandlers(get_eventHandlers_override, None, EventManager)              
        eventManager = EventManager()
        eventManager.start()
        eventManager.add_event(Event('A', '01'))    
        while eventManager.is_at_least_one_dispatcher_alive():
            print "waiting"
        assert not eventManager.has_events()

def get_eventHandlers_override(self):
    eventHandlerA = EventHandlerA()
    eventHandlerB = EventHandlerB()
    eventHandlerC = EventHandlerC()
    eventHandlers = {'A':eventHandlerA, 'B':eventHandlerB, 'C':eventHandlerC}
    return eventHandlers
                        
class EventHandlerA(BaseEventHandler):
    def do(self, data):
        print 'Hello from yarra.handler A: data = ' + str(data)        
        em = EventManager()
        em.add_event(Event('B', 100))
class EventHandlerB(BaseEventHandler):
    def do(self, data):
        print 'Hello from yarra.handler B: data = ' + str(data)
        EventManager().add_event(Event('C', 200))
class EventHandlerC(BaseEventHandler):
    def do(self, data):
        print 'Hello from yarra.handler C: data = ' + str(data)
        EventManager().add_event(Event('STOP', 'STOPPING'))
        EventManager().add_event(Event('STOP', 'STOPPING'))  
        
if __name__ == "__main__":

    unittest.main()